package autoz.core.launching;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.plugin.AbstractUIPlugin;

public abstract class AbstractSourceConfigurationTab extends AbstractLaunchConfigurationTab implements ModifyListener{
		
		public abstract void createControl(Composite parent) ;
		
		public String getActiveEditorPath(){
			IWorkbench wb = PlatformUI.getWorkbench();
			IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
			IWorkbenchPage page = win.getActivePage();
			IEditorPart part = page.getActiveEditor();
			
			if (part.getEditorInput() instanceof FileEditorInput){
				FileEditorInput fileEditorInput = (FileEditorInput) part.getEditorInput();
				String path = fileEditorInput.getFile().getFullPath().toOSString();
				if (path.endsWith(getFileExtension())){
					return path;
				}
			}
			
			return "";
			
		}
		
		public String getActiveEditorName(){
			IWorkbench wb = PlatformUI.getWorkbench();
			IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
			IWorkbenchPage page = win.getActivePage();
			IEditorPart part = page.getActiveEditor();
			
			if (part != null && part.getEditorInput() instanceof FileEditorInput){
				FileEditorInput fileEditorInput = (FileEditorInput) part.getEditorInput();
				String path = fileEditorInput.getFile().getFullPath().toOSString();
				if (path.endsWith(getFileExtension())){
					String fileName = fileEditorInput.getFile().getName();
					fileName = fileName.substring(0,fileName.length() - getFileExtension().length() + -1);
					return fileName;
				}
			}
			
			return "";
			
		}


		public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
			String activeEditorName = getActiveEditorName();
			if (activeEditorName.length() > 0){
				configuration.rename(newLaunchConfigurationName(activeEditorName));
				configuration.setAttribute("model", getActiveEditorPath());
			}
			
			PlatformUI.getWorkbench();
			
		}
		
		// Return a new launch configuration name that does not
		// already exists
		protected String newLaunchConfigurationName(String fileName) {
			
			if (!launchConfigurationExists(fileName)) return fileName;
			
			int i = 1;
			while (true){
				String configurationName = fileName + " (" + i + ")";
				if (!launchConfigurationExists(configurationName)) return configurationName;
				i++;
			}
		}
		
		private boolean launchConfigurationExists(String name) {
			ILaunchConfiguration[] cs = new ILaunchConfiguration[]{};
			try {
				cs = getLaunchManager().getLaunchConfigurations();
			}
			catch (CoreException ex){
				ex.printStackTrace();
			}
			for (int i=0;i<cs.length;i++){
				ILaunchConfiguration c = cs[i];
				if (c.getName().equals(name)){
					return true;
				}
			}
			return false;
		}
		
		public void initializeFrom(ILaunchConfiguration configuration) {
//			try {
//				modelFile.setText(configuration.getAttribute(getSourceAttributeName(), ""));
				canSave();
				updateLaunchConfigurationDialog();
//			} catch (CoreException e) {
//				//Ignore
//			}
		}

		public abstract void performApply(ILaunchConfigurationWorkingCopy configuration) ;
		
		public String getName() {
			return getTitle();
		}
		
		public abstract boolean canSave();
		
		public class SelectSourceListener implements Listener{
			
			Text text;
			
			public SelectSourceListener(Text text) {
				this.text = text;
			}
			
			public void handleEvent(Event event) {
				String selected = 
					BrowseWorkspaceUtil.browseFilePath(getShell(),
					getSelectionTitle()
					,getSelectionSubtitle()
					,getFileExtension()
					,null);//getPlugin().createImage(getImagePath()));
				
				if (selected!=null) text.setText(selected);
			}
			
		}
 
		public void modifyText(ModifyEvent e) {
			canSave();
			updateLaunchConfigurationDialog();
		}
		
		public abstract AbstractUIPlugin getPlugin();
		
		public abstract String getImagePath();
		
		public abstract String getFileExtension();
		
		public abstract String getSelectionTitle();
		
		public abstract String getSelectionSubtitle();
		
		public String getTitle() {
			return "Model";
		}
		
		public String getSourceAttributeName() {
			return "source";
		}
}
